﻿Imports System.Globalization
Imports System.IO

Partial Public Class Payment_Report
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "PAYMENT REPORT "
            txtDate.Text = Date.Now.AddDays(-14).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtDateTo.Text = Date.Now.AddDays(14).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            Dim ds As DataTable = clsGet.Get_Bank("")
            If (ds.Rows.Count > 0) Then
                With ddlBank
                    .ClearSelection()
                    .Items.Clear()
                    .DataTextField = "Bank_Name"
                    .DataValueField = "Bank_SAP"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุเวลาเริ่มต้น!!');", True)
            Exit Sub
        End If
        If (txtDateTo.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุเวลาสิ้นสุด!!');", True)
            Exit Sub
        End If
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dt As DataTable = clsGet.Get_Payment_Report(txtDate.Text.Split(" ")(2).Trim() & CDate(txtDate.Text).ToString("MMdd"), txtDateTo.Text.Split(" ")(2).Trim() & CDate(txtDateTo.Text).ToString("MMdd"), txtCustomer.Text, ddlBank.SelectedValue, radioTypePay.SelectedValue, radioEPay.SelectedValue)
        If (dt.Rows.Count > 0) Then
            dsReport.Tables.Add(dt.Copy)
            dsReport.AcceptChanges()
            ReportName = "Payment_Report.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Payment_P_E_PAY"
            paraName = "DateTimeNow"
            paraValue = DateTime.Now.ToString()

            Orientation = False
            reportType = radioType.SelectedValue

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Payment_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูล!!');", True)
        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnCusLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCusLoad.Click
        txtCustomer.Text = Session("Customer_NoCNDN")
    End Sub

    Private Sub btnSearchCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchCustomer.Click
        Session("txtCUSCNDN") = txtCustomer.Text
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "showCus();", True)
        ModalPopupExtender1.Show()
    End Sub
End Class